package com.zxp.security.config;

import com.zxp.common.core.security.authentication.MobileAuthenticationSecurityConfig;
import com.zxp.common.core.security.authorize.MyAuthorizationConfigurerService;
import com.zxp.common.core.security.authorize.ZxpAuthorizationConfigurerManager;
import com.zxp.common.core.security.authorize.impl.ZxpAuthorizationConfigurerManagerImpl;
import com.zxp.common.core.security.filter.ValidateCodeFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import java.util.List;

/**
 * 配置资源服务器
 * @author shollin
 * @desc
 * @date 2021/7/11/011 17:39
 */
@Configuration
@RequiredArgsConstructor
@EnableResourceServer
public class MyResouceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    private ValidateCodeFilter validateCodeFilter;

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    private MobileAuthenticationSecurityConfig mobileAuthenticationSecurityConfig;

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private List<MyAuthorizationConfigurerService> authorizationConfigurerServices;

    @Bean
    public ZxpAuthorizationConfigurerManager authorizationConfigurerManager(){
        return new ZxpAuthorizationConfigurerManagerImpl(authorizationConfigurerServices);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        // 所有的请求都需要认证
        http.authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers(HttpMethod.GET,"/admin/**").hasRole("ADMIN") // 对应角色：ROLE_ADMIN
                .antMatchers("/user/**").hasRole("USER");

        authorizationConfigurerManager().config(http.authorizeRequests()); // 其它的请求需要认证

        // 表单登陆配置
        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class);

        http.formLogin()
                .loginPage("/login") // 登陆页get请求login, 登陆post请求login
                .permitAll()
                .successHandler(authenticationSuccessHandler) // 表单登陆成功、失败处理器
                .failureHandler(authenticationFailureHandler);

        // 安全策略配置
        http.csrf().disable();
        http.cors();

        http.sessionManagement().disable();

        http.apply(mobileAuthenticationSecurityConfig);

    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenStore(tokenStore);
    }
}
